using MongoDB.Bson;
using TangMonk.Models;
using TangMonk.Service;

namespace TangMonk.Query;

public class UserMutation
{
    private readonly UserBaseService _baseService;

    public UserMutation(UserBaseService baseService)
    {
        _baseService = baseService;
    }

    public Task<RequestUserResult> DebugNew(User user)
    {
        var requestResult = new RequestResult("create user")
        {
            Success = true,
            Result = "Successfully"
        };
        user.Id = ObjectId.GenerateNewId().ToString();
        var result = new RequestUserResult(new List<User> { user }, requestResult);
        return Task.FromResult<RequestUserResult>(result);
    }

    public async Task<RequestUserResult> New(User user)
    {
        var requestResult = new RequestResult("create user");
        try
        {
            await _baseService.CreateOne(user);
            requestResult.Success = true;
            requestResult.Result = "Successfully";
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            requestResult.Success = false;
            requestResult.Result = e.ToString();
        }

        var result = new RequestUserResult(new List<User> { user }, requestResult);

        return result;
    }

    public async Task<RequestUserResult> Update(User user)
    {
        var requestResult = new RequestResult("update user");
        try
        {
            await _baseService.UpdateOne(user);
            requestResult.Success = true;
            requestResult.Result = "Successfully";
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            requestResult.Success = false;
            requestResult.Result = e.ToString();
        }

        var result = new RequestUserResult(new List<User> { user }, requestResult);

        return result;
    }

}